<template>
  <div class="login-contain">
    <van-nav-bar class="my-nav-bar" title="登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        required
        label="手机号码"
        placeholder="请输入手机号码"
        :rules="[
          { required: true, message: '请填写正确的手机号' },
          {
            pattern: /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/,
            message: '请输入正确手机号格式'
          }
        ]"
      />
      <van-field
        v-model="code"
        name="code"
        required
        label="验证码"
        placeholder="请输入验证码"
        :rules="[
          { required: true, message: '请填写正确验证码' },
          {
            pattern: /\d{6}/,
            message: '请输入正确验证码格式'
          }
        ]"
      />
      <div style="margin: 16px;">
        <van-button
          :loading="isLoading"
          :disabled="isLoading"
          loading-text="登陆中"
          round
          block
          type="info"
          native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import { userLogin } from '../../api/login.js'
import { saveToken } from '../../utils/token.js'

export default {
  name: 'login',
  data () {
    return {
      mobile: '15502538918',
      code: '246810',
      isLoading: false
    }
  },

  methods: {
    onSubmit (values) {
      // console.log('submit', values)
      this.isLoading = true
      userLogin(values)
        .then(res => {
          console.log(res)
          this.isLoading = false
          this.$toast.success('登录成功')
          saveToken(res.data.data)
          if (this.$route.query.back === undefined) {
            this.$router.push('/home')
          } else {
            this.$router.push(this.$route.query.back)
          }
        })
        .catch(res => {
          console.log(res)
          this.isLoading = false
          this.$toast.fail('验证码有误')
        })
    }
  }
}
</script>
<style lang="less">
// .login-contain .my-nav-bar {
//   background-color: #1989fa;
//   .van-nav-bar__title {
//     color: #fff;
//   }
// }
</style>
